package glbjavatest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author nkeni
 */
public class IPInit {

	public static final String COLUMN_SEPERATOR_REGEX = "\\|";
	public static TreeSet<String> placeSet = new TreeSet<String>();
	public static TreeSet<String> areaSet = new TreeSet<String>();
	public static HashMap<String, String> ipMap = new HashMap<String, String>();

	public static void main(String[] args) throws Exception {
		File f = new File("e:\\\\opt\\\\ip2018.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
		int flag = 0;
		while (br.ready()) {
			String line = br.readLine();

			String[] str;
			str = line.split(COLUMN_SEPERATOR_REGEX, -1);
			// placeSet.add();
			int count = 0;
			System.out.println(line);

			for (String ss : str) {
				//System.out.println(count + ":" + ss);
				if ((count == 6 || count == 7 || count == 8 || count == 10) && ss != null && ss.trim().length() > 0) {
					placeSet.add(ss);
				}
				count++;
			}
			String ipSeg = str[0] + "|" + str[1];
			String area = str[10] + "|" + str[6] + "|" + str[7] + "|" + str[8];
			areaSet.add(area);
			ipMap.put(ipSeg, area);
		}
		br.close();
		writePlace();
		// writeFile(list);
	}

	public static void writePlace() {

		FileWriter fw = null;
		BufferedWriter writer = null;
		HashMap<String, String> placeMap = new HashMap<String, String>();
		HashMap<String, String> areaMap = new HashMap<String, String>();

		try {
			File file = new File("e:\\opt\\newplace.txt");
			fw = new FileWriter(file);
			writer = new BufferedWriter(fw);
			int i = 0;
			for (String place : placeSet) {
				writer.write((i ) + "|" + place);
				placeMap.put(place, "" + (i ));
				writer.newLine();// 换行
				i++;
			}
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fw.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		try {
			File file = new File("e:\\opt\\newarea.txt");
			fw = new FileWriter(file);
			writer = new BufferedWriter(fw);
			int i = 0;
			for (String area : areaSet) {
				String[] areaArray = area.split(COLUMN_SEPERATOR_REGEX, -1);
				String areaStr = "";
				for (String ss : areaArray) {
					if (ss != null && ss.length() > 0) {
						String index = placeMap.get(ss);
						if (index==null||"null".equals(index)) {
							System.out.println("eeeee:"+ss+","+i+","+area);
						}
						areaStr = areaStr + "|" + index;
					} else {
						areaStr = areaStr + "|" + "-1";
					}
				}
				writer.write((i ) + areaStr);
				areaMap.put(area, "" + (i ));
				writer.newLine();// 换行
				i++;
			}
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fw.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		try {
			File file = new File("e:\\opt\\newip.txt");
			fw = new FileWriter(file);
			writer = new BufferedWriter(fw);
			int i = 0;
			for (String ipSeg : ipMap.keySet()) {
				String areaStr = ipMap.get(ipSeg);
				String ipStr = "";
				if (areaStr != null && areaStr.length() > 0) {
					String index = areaMap.get(areaStr);
					ipStr = index + "|" + ipSeg;
				} else {
					ipStr = -1 + "|" + ipSeg;
					System.out.println("error");
				}

				writer.write((i) + "|" + ipStr);
				writer.newLine();// 换行
				i++;
			}
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fw.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void writeFile(List<String[]> allList) {

		FileWriter fw = null;
		BufferedWriter writer = null;
		HashMap<String, String[]> keyMap = new HashMap<>();
		HashMap<String, String> brandMap = new HashMap<>();
		HashMap<String, String> issOrgMap = new HashMap<>();
		HashMap<String, String> typeMap = new HashMap<>();
		HashMap<String, String> categoryMap = new HashMap<>();
		HashMap<String, String[]> countryMap = new HashMap<>();
		int row = 0;
		LinkedList<String[]> cardBinList = new LinkedList<>();
		for (String[] line : allList) {
			row++;
			if (row == 1) {
				continue;
			}
			String cardbin = line[0];
			String brand = line[1];
			String issOrg = line[2];
			String type = line[3];
			String category = line[4];
			String country = line[5];
			String countryA2 = line[6];
			String countryA3 = line[7];
			String countryISO = line[8];
			String[] newStr = new String[] { "", "", "", "", "", "" };
			newStr[0] = cardbin;
			// brand================
			if (brand == null || brand.trim().length() == 0) {
				newStr[1] = "";
			} else {
				String brandIndex = brandMap.get(brand);
				if (brandIndex == null || brandIndex.trim().length() == 0) {
					int bi = brandMap.size() + 1;
					brandMap.put(brand.trim(), bi + "");
					newStr[1] = bi + "";
				} else {
					newStr[1] = brandIndex;
				}
			}
			// issOrg ================
			if (issOrg == null || issOrg.trim().length() == 0) {
				newStr[2] = "";
			} else {
				String issOrgIndex = issOrgMap.get(issOrg);
				if (issOrgIndex == null || issOrgIndex.trim().length() == 0) {
					int oi = issOrgMap.size() + 1;
					issOrgMap.put(issOrg.trim(), oi + "");
					newStr[2] = oi + "";
				} else {
					newStr[2] = issOrgIndex;
				}
			}
			// type ================
			if (type == null || type.trim().length() == 0) {
				newStr[3] = "";
			} else {
				String typeIndex = typeMap.get(type);
				if (typeIndex == null || typeIndex.trim().length() == 0) {
					int ti = typeMap.size() + 1;
					typeMap.put(type.trim(), ti + "");
					newStr[3] = ti + "";
				} else {
					newStr[3] = typeIndex;
				}
			}
			// category ================
			if (category == null || category.trim().length() == 0) {
				newStr[4] = "";
			} else {
				String categoryIndex = categoryMap.get(category);
				if (categoryIndex == null || categoryIndex.trim().length() == 0) {
					int cai = categoryMap.size() + 1;
					categoryMap.put(category.trim(), cai + "");
					newStr[4] = cai + "";
				} else {
					newStr[4] = categoryIndex;
				}
			}
			// country ================
			if (country == null || country.trim().length() == 0) {
				newStr[5] = "";
			} else {
				String[] countryArray = countryMap.get(country);
				if (countryArray == null || countryArray.length == 0) {
					int coi = countryMap.size() + 1;
					String[] couA = new String[5];
					couA[0] = country.trim();
					couA[1] = countryA2.trim();
					couA[2] = countryA3.trim();
					couA[3] = countryISO.trim();
					couA[4] = coi + "";
					countryMap.put(country.trim(), couA);
					newStr[5] = coi + "";
				} else {
					newStr[5] = countryArray[4];
				}
			}
			cardBinList.add(newStr);
		}
		try {
			File file = new File("D:\\work\\project\\gitdoc\\newcardbin.txt");
			fw = new FileWriter(file);
			writer = new BufferedWriter(fw);
			int brandCount = brandMap.size();
			int typeCount = typeMap.size();
			int issOrgCount = issOrgMap.size();
			int categoryCount = categoryMap.size();
			int countryCount = countryMap.size();
			int cardBinCount = cardBinList.size();
			writer.write("===brand===");
			writer.newLine();// 换行
			String[] brandArray = new String[brandMap.size()];
			for (String brandKey : brandMap.keySet()) {
				int i = Integer.parseInt(brandMap.get(brandKey));
				brandArray[i - 1] = brandKey;
			}
			for (int i = 0; i < brandArray.length; i++) {
				writer.write((i + 1) + "|" + brandArray[i]);
				writer.newLine();// 换行
			}
			writer.write("===issOrg===");
			writer.newLine();// 换行
			String[] issOrgArray = new String[issOrgMap.size()];
			for (String issOrgKey : issOrgMap.keySet()) {
				int i = Integer.parseInt(issOrgMap.get(issOrgKey));
				issOrgArray[i - 1] = issOrgKey;
			}
			for (int i = 0; i < issOrgArray.length; i++) {
				writer.write((i + 1) + "|" + issOrgArray[i]);
				writer.newLine();// 换行
			}
			writer.write("===type===");
			writer.newLine();// 换行
			String[] typeArray = new String[typeMap.size()];
			for (String typeKey : typeMap.keySet()) {
				int i = Integer.parseInt(typeMap.get(typeKey));
				typeArray[i - 1] = typeKey;
			}
			for (int i = 0; i < typeArray.length; i++) {
				writer.write((i + 1) + "|" + typeArray[i]);
				writer.newLine();// 换行
			}
			writer.write("===category===");
			writer.newLine();// 换行
			String[] categoryArray = new String[categoryMap.size()];
			for (String categoryKey : categoryMap.keySet()) {
				int i = Integer.parseInt(categoryMap.get(categoryKey));
				categoryArray[i - 1] = categoryKey;
			}
			for (int i = 0; i < categoryArray.length; i++) {
				writer.write((i + 1) + "|" + categoryArray[i]);
				writer.newLine();// 换行
			}
			writer.write("===country===");
			writer.newLine();// 换行
			String[] counrtyArray = new String[countryMap.size()];
			for (String countryKey : countryMap.keySet()) {
				String[] countryV = countryMap.get(countryKey);
				int i = Integer.parseInt(countryV[4]);
				counrtyArray[i - 1] = countryV[0] + "|" + countryV[1] + "|" + countryV[2] + "|" + countryV[3];
			}
			for (int i = 0; i < counrtyArray.length; i++) {
				writer.write((i + 1) + "|" + counrtyArray[i]);
				writer.newLine();// 换行
			}
			writer.write("===cardBin===");
			writer.newLine();// 换行
			for (String[] cardBinStr : cardBinList) {
				writer.write(cardBinStr[0] + "|" + cardBinStr[1] + "|" + cardBinStr[2] + "|" + cardBinStr[3] + "|"
						+ cardBinStr[4] + "|" + cardBinStr[5]);
				writer.newLine();// 换行
			}
			writer.flush();
			System.out.println("cardBin:" + cardBinCount + ",brand:" + brandCount + ",issOrg:" + issOrgCount + ",type:"
					+ typeCount + ",category:" + categoryCount + ",country:" + countryCount);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fw.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}
